home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10200 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: rcp6.elan.af.mil!rscernix!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: malloc question
  5. Date: 12 Mar 96 23:58:31 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <danpop.826675111@rscernix>
  8. References: <4htonk$350@news.hklink.net> <danpop.826488975@rscernix> <4i44vk$g16@rigel.rz.uni-ulm.de>
  9. NNTP-Posting-Host: ues5.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11.  
  12. In <4i44vk$g16@rigel.rz.uni-ulm.de> peinel@faw.uni-ulm.de (Gertraud Peinel) writes:
  13.  
  14. >In article <danpop.826488975@rscernix>, danpop@mail.cern.ch (Dan Pop) writes:
  15. >|> 
  16. >|> You DON'T have to cast the value returned by malloc, casting it is actually
  17. >|> a BAD idea.  But you DO have to include <stdlib.h> in any source file
  18. >|> which calls malloc.
  19. >
  20. >Hmmm, and what should I do with this (especially when I have to use these
  21. >compilers) ? :
  22. >
  23. >  sun5:/users/peinel/clang(36)> uname -a
  24. >  SunOS sun5 4.1.3 3 sun4c
  25. >  sun5:/users/peinel/clang(35)> cat oh.c 
  26. >  #include <stdlib.h>
  27. >
  28. >  typedef struct item {
  29. >    int val;
  30. >    struct item *next;
  31. >  } ITEM, *PITEM;
  32. >
  33. >  main()
  34. >  {
  35. >    PITEM head, current;
  36. >    head= malloc(sizeof(ITEM));
  37. >    head->val=1;
  38. >  }
  39. >  sun5:/users/peinel/clang(29)> cc oh.c
  40. >  "oh.c", line 11: warning: illegal pointer combination
  41. >  sun5:/users/peinel/clang(34)> which cc
  42. >  /bin/cc
  43. >  sun5:/users/peinel/clang(32)> CC oh.c
  44. >  CC  mist.c:
  45. >  "oh.c", line 11: error: no standard conversion of  char * to  struct item *
  46. >  "oh.c", line 10: warning:  current not used
  47. >  1 error
  48. >  sun5:/users/peinel/clang(33)> which CC
  49. >  /com/owc++/bin/CC
  50. >
  51. >Without casting ?
  52.  
  53. Neither of these compilers being a (standard) C compiler, the obvious
  54. answer is to get a C compiler.  It makes absolutely no sense to expect
  55. standard C code to compile on a non-standard C compiler.  BTW, have you
  56. tried the above code on a Fortran or Pascal compiler?  They're almost as
  57. relevant to this code as the compilers you tried.
  58.  
  59. If you have to use a K&R C compiler, write correct K&R C code.
  60. If you have to use a C++ compiler, write correct C++ code.
  61.  
  62. It's a very poor idea to write C code which can be compiled by non-C
  63. compilers as well, because you cannot take advantage of the features which
  64. are unique to C, and you end up coding in a sort of C-- language :-)
  65.  
  66. Dan
  67. --
  68. Dan Pop
  69. CERN, CN Division
  70. Email: danpop@mail.cern.ch 
  71. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  72.